﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
public partial class admin_UserControl_Prov_City_Dist : System.Web.UI.UserControl
{
    public string DistrictId
    {
        get { return ddlDistrict.SelectedValue; }
        set 
        {
            //如果已经绑定，直接设置
            if (ddlDistrict.Items.FindByValue(value) != null)
            {
                ddlDistrict.SelectedValue = value;
                return;
            }
            //绑定并地区
            Model.T_District districr = new T_District().GetModel(Convert.ToInt32(value));
            PublicControlBind.Bind_DLL_DistrictList(ddlDistrict, districr.CityID.ToString());
            ddlDistrict.SelectedValue = value;
            //绑定并设置城市
            Model.T_City city = new T_City().GetModel(Convert.ToInt32(districr.CityID.ToString()));
            PublicControlBind.Bind_DLL_CityList(ddlCity, city.ProID.ToString());
            ddlCity.SelectedValue = city.Id.ToString();
            //绑定并设置省份
            if (ddlProvince.Items.FindByValue(city.ProID.ToString()) == null)
            {
                PublicControlBind.Bind_DLL_ProvinceList(ddlProvince);
            }
            ddlProvince.SelectedValue = city.ProID.ToString();
        }
    }
    public DropDownList DDL_City 
    {
        get { return ddlCity; }
    }
    public DropDownList DDL_District
    {
        get { return ddlDistrict; }
    }
    public DropDownList DDL_Province
    {
        get { return ddlProvince; }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            UIInit();
        }
    }

    private void UIInit()
    {
        if (ddlDistrict.Items.Count > 1 && ddlDistrict.SelectedValue != "-1")
        {
            return;
        }
        PublicControlBind.Bind_DLL_ProvinceList(ddlProvince);
        PublicControlBind.Bind_DLL_CityList(ddlCity, ddlProvince.SelectedValue);
        PublicControlBind.Bind_DLL_DistrictList(ddlDistrict,ddlCity.SelectedValue);
    }
    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        PublicControlBind.Bind_DLL_CityList(ddlCity, ddlProvince.SelectedValue);
        PublicControlBind.Bind_DLL_DistrictList(ddlDistrict, ddlCity.SelectedValue);
    }
    protected void dllCity_SelectedIndexChanged(object sender, EventArgs e)
    {
        PublicControlBind.Bind_DLL_DistrictList(ddlDistrict, ddlCity.SelectedValue);
    }
}